/**   
 * Copyright © 2016 公司名. All rights reserved.
 * 
 * @Title: CartServiceImpl.java 
 * @Prject: LianniuWeb
 * @Package: com.lianniu.web.buyerProduct.service.impl
 * @Description: TODO
 * @author: lzz   
 * @date: 2016年9月2日 下午12:23:17 
 * @version: V1.0   
 */
package com.lianniu.web.buyerProduct.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.buyerProduct.dao.ICartDao;
import com.lianniu.web.buyerProduct.model.CartVo;
import com.lianniu.web.buyerProduct.service.ICartService;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.product.model.Page;

/** 
 * @ClassName: CartServiceImpl 
 * @author: lzz
 * @date: 2016年9月2日 下午12:23:17  
 */
@Service("cartService")
public class CartServiceImpl implements ICartService {
    public static final Logger LOGGER = Logger.getLogger(CartServiceImpl.class);
    
    @Autowired
    private ICartDao cartDao;
    
    @Override
    public BaseResult addCart(CartVo cartVo) {
        LOGGER.info("CartServiceImpl addCart begin ");
        int i = 0;
        CartVo cart = cartDao.getCartBySkuId(cartVo.getUserId(), cartVo.getSupplyAreaID());
        if(null != cart){
            cart.setProductNum(cartVo.getProductNum());
            cart.setModifyTime(new Date());
            cart.setModifyUser(cartVo.getUserId());
            i = cartDao.updateSum(cart);
        }else{
            cartVo.setDelFlag("1");
            cartVo.setAddTime(new Date());
            cartVo.setOrderType("01");
            i = cartDao.insert(cartVo);
        }
        LOGGER.info("CartServiceImpl addCart end ");
        return BaseResult.success(i);
    }
    @Override
    public BaseResult getCartList(String userId,Integer cartPage) {
        LOGGER.info("CartServiceImpl getCartList begin ");
        List<CartVo> list = null;
        if(null != cartPage){//分页显示购物车列表
            Map<String, Object> param = new HashMap<>();
            param.put("userId", userId);
            int count = 0;
            Page page =null;
            
           try {
               count = cartDao.getCartCount(userId);
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.info(ErrorCode.SQLExceptionRetInfo);
                return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
            }
            page = new Page(count, cartPage,6);
            param.put("page", page);
            try {
                if (cartPage <= page.getPageSum()) {
                    list = cartDao.getCartListPager(param);
                }
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.info(ErrorCode.SQLExceptionRetInfo);
                return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
            }
        }else{//查购物车所有
            list = cartDao.getCartList(userId);
        }
        
        LOGGER.info("CartServiceImpl getCartList end ");
        return BaseResult.success(list);
    }
    @Override
    public BaseResult editProSum(CartVo cartVo) {
        LOGGER.info("CartServiceImpl editProSum begin ");
        cartVo.setModifyTime(new Date());
        cartVo.setModifyUser(cartVo.getUserId());
        int i = cartDao.updateSum(cartVo);
        LOGGER.info("CartServiceImpl editProSum end ");
        return BaseResult.success(i);
    }
    @Override
    public BaseResult isExits(String supplyAreaID) {
        LOGGER.info("CartServiceImpl isExits begin ");
        boolean b = cartDao.isExists(supplyAreaID);
        LOGGER.info("CartServiceImpl isExits end ");
        return BaseResult.success(b+"");
    }
    @Override
    public BaseResult delCart(String cartIds) {
      LOGGER.info("CartServiceImpl delCart begin ");
      String[] s = cartIds.split(",");
      int i = cartDao.delete(s);
      LOGGER.info("CartServiceImpl delCart end ");
      return BaseResult.success(i);
    }
}
